package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.mapper.CardMapper;
import com.jieshuibao.model.db.Card;
import com.jieshuibao.model.db.CardExample;
import com.jieshuibao.model.db.RedEnvelopes;
import com.jieshuibao.util.DateUtils;
import com.jieshuibao.util.PayTypeUtil;

@Service
public class CardService{
	@Resource
	private CardService cardService;
	@Resource
	private RedEnvelopesService redEnvelopesService;
	
	@Resource
	private CardMapper cardMapper;
	
	public void addCard(Card card) {
		cardMapper.insertSelective(card);
	}
	
	public void delCardById(Long id) {
		cardMapper.deleteByPrimaryKey(id);
	}
	
	public void updateCard(Card card) {
		cardMapper.updateByPrimaryKeySelective(card);
	}
	
	public Card getCardById(Long id) {
		return cardMapper.selectByPrimaryKey(id);
	}
	
	public List<Card> getPageCard(CardExample cardExample) {
		return cardMapper.selectPage(cardExample);
	}
	
	public int countCard(CardExample cardExample) {
		return cardMapper.countByExample(cardExample);
	}
	
	public BaseRes<String> bindingQACard(String cookieId,String userId,Card card){
		BaseRes<String> baseRes = new BaseRes<String>();
		/**
		 * 判读问答卡信息是否合法
		 * 1.查询问答卡是否存在
		 * 2.判断防伪码是否正确
		 * 3.判断问答卡是否已被激活
		 */
		Card cardById = cardService.getCardById(card.getId());
		//1.查询问答卡是否存在
		if(cardById == null || "".equals(cardById)){
			baseRes.setCode("0");
			baseRes.setDes("问答卡卡号错误或不存在");
			return baseRes;
		}
		//2.判断防伪码是否正确
		if(!cardById.getSecurityCode().equals(card.getSecurityCode())){
			baseRes.setCode("0");
			baseRes.setDes("防伪码错误");
			return baseRes;
		}
		//3.判断问答卡是否已被激活
		if(!"0".equals(cardById.getStatus())){
			baseRes.setCode("0");
			baseRes.setDes("此问答卡已被激活，无法使用");
			return baseRes;
		}
		Date nowDate = new Date();
		if(DateUtils.compareDateYMD(nowDate, cardById.getLimitActiveTime())){
			//激活截止日期小于当前时间
			baseRes.setCode("0");
			baseRes.setDes("此问答卡已过期");
			return baseRes;
		}
		/**
		 * 将问答卡绑定用户
		 * 1.将问答卡金额赋值增加到用户问答余额
		 * 2.将问答卡渠道绑定到用户
		 * 3.交易流水
		 */
		//对当前用户绑定固定面值问答红卡
		RedEnvelopes redEnvelopes = new RedEnvelopes();
		redEnvelopes.setMoney(cardById.getAmount());//金额
		redEnvelopes.setUserPhoneNum(userId);//领取红包人的用户主键
		redEnvelopes.setRedType(PayTypeUtil.REDENVELPES_BINDINGQACARD);//红包类型
		/**
		 * 1.将问答卡金额赋值增加到用户问答余额
		 * 2.将问答卡渠道绑定到用户
		 * 3.交易流水
		 */ 
		redEnvelopesService.addQACardRedEnvelopes(redEnvelopes,cardById.getChannelId());
		//激活问答卡
		cardService.updateCardForActivation(userId, cardById.getId());
		baseRes.setCode("1");
		baseRes.setDes("绑定成功");
		return baseRes;
	}
	/**
	 * 激活问答卡
	 * @param userId 用户主键
	 * @param cardId 问答卡主键
	 */
	public int updateCardForActivation(String userId,Long cardId) {
		Card card = new Card();
		card.setId(cardId);
		card.setStatus("1");//问答卡状态更改为  已激活
		card.setActiveDate(new Date());//问答卡激活时间
		card.setUserId(userId);//绑定的用户id
		return cardMapper.updateByPrimaryKeySelective(card);
	}
	
}
